package com.rongshu.sendmessage.view.bottomView;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.rongshu.sendmessage.R;
import com.rongshu.sendmessage.adapter.CityAdapter;
import com.rongshu.sendmessage.adapter.QuAdapter;
import com.rongshu.sendmessage.adapter.ShengAdapter;
import com.rongshu.sendmessage.base.MyApplication;
import com.rongshu.sendmessage.bean.LocationBean;
import com.rongshu.sendmessage.util.ClientPublicParamUtil;
import com.rongshu.sendmessage.util.DialogUtil;
import com.rongshu.sendmessage.util.ToastUtils;
import com.rongshu.sendmessage.util.httpUtil.HYContent;
import com.rongshu.sendmessage.util.httpUtil.IModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.rongshu.sendmessage.util.DialogUtil.hideProgress;

public class BottomSelectCityDialog implements View.OnClickListener{
    private final IEditAreaClick callBack;
    private final String userId;
    private final String source;
    private View inflate;
    private Context mContext;
    private BottomSheetDialog bsd1;

    private int shengPosition;
    private int shiPosition;
    private ListView listView,listView1;
    private ShengAdapter shengAdapter;
    private CityAdapter shiAdapter;
    private List<LocationBean> shengList=new ArrayList<>();
    private List<LocationBean> shiList=new ArrayList<>();
    private String defaultLocation;
    private View iv_close;
    private View btn_ok;
    private LocationBean locationBean;

    public BottomSelectCityDialog(String userId, String source, IEditAreaClick callBack){
        this.callBack=callBack;
        this.userId=userId;
        this.source=source;
    }
    public View showDialog(Context context) {
        //自定义dialog显示布局
        inflate = LayoutInflater.from(context).inflate(R.layout.select_city_bottom_view, null);
        mContext=context;
        initAreaView(inflate);
        initAreaData(0,"");

        //自定义dialog显示风格
        bsd1=new BottomSheetDialog(mContext);
        inflate.post(new Runnable() {
            @Override
            public void run() {
                BottomSheetBehavior behavior= BottomSheetBehavior.from(bsd1.findViewById(R.id.design_bottom_sheet));
                behavior.setHideable(false);//此处设置表示禁止BottomSheetBehavior的执行
                behavior.setPeekHeight(1500);
            }
        });

        bsd1.setContentView(inflate);
        bsd1.getWindow().findViewById(R.id.design_bottom_sheet)
                .setBackgroundResource(android.R.color.transparent);
        bsd1.setCanceledOnTouchOutside(false);
//        bsd1.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

        bsd1.show();
        bsd1.setCanceledOnTouchOutside(false);
        return inflate;
    }
    public boolean isShow(){
        return bsd1.isShowing();
    }
    private void initAreaView(View inflate) {
        listView = inflate.findViewById(R.id.listView);
        listView1 = inflate.findViewById(R.id.listView1);
        iv_close = inflate.findViewById(R.id.iv_close);
        iv_close.setOnClickListener(this);
        btn_ok = inflate.findViewById(R.id.btn_ok);
        btn_ok.setOnClickListener(this);
        //        省
        listView1.setVisibility(View.GONE);
        shengAdapter=new ShengAdapter(mContext,shengList);
        listView.setAdapter(shengAdapter);
        listView.setOnItemClickListener((parent, view, position, id) -> {
            shengPosition=position;
            LocationBean locationShengBean = shengList.get(position);
            for (int i = 0; i < shengList.size(); i++) {
                shengList.get(i).setSelect(i == position);
            }
            listView1.setVisibility(View.VISIBLE);
            shengAdapter.notifyDataSetChanged();
            if (locationShengBean.getName().contains("市")||locationShengBean.getName().contains("特别行政区")){
                locationBean=locationShengBean;
                return;
            }
            locationBean=null;
            initAreaData(1,locationShengBean.getId()+"");
        });
//  市
        shiAdapter=new CityAdapter(mContext,shiList);
        listView1.setAdapter(shiAdapter);
        listView1.setOnItemClickListener((parent, view, position, id) -> {
            shiPosition=position;
             locationBean = shiList.get(position);
            for (int i = 0; i < shiList.size(); i++) {
                shiList.get(i).setSelect(i == position);
            }

            listView1.setVisibility(View.VISIBLE);
            shiAdapter.notifyDataSetChanged();
        });

        Button btn_location = inflate.findViewById(R.id.btn_location);

        if ("location".equals(source)){
            btn_location.setVisibility(View.VISIBLE);
            String location = MyApplication.mSettings.getString(HYContent.CITY_LOCATION_NAME, "");
            String cityCode = MyApplication.mSettings.getString(HYContent.CITY_CODE, "");
            btn_location.setText(location);
            btn_location.setOnClickListener(view->{
                MyApplication.editor.putString(HYContent.LOCATION, location);
                MyApplication.editor.putString(HYContent.CITY_NAME, location);
                MyApplication.editor.commit();
                callBack.editArearClick(location,cityCode);
                bsd1.dismiss();
            });
        }else{
            btn_location.setVisibility(View.GONE);

        }
    }
    //    地区选择
    private void initAreaData(final int type,String id) {
        Map<String, String> param = new HashMap<String, String>();
        DialogUtil.showProgress(mContext);
        param.put("parentId",id);
        param.put("clientPublicParam", ClientPublicParamUtil.getPublicParams(mContext));
        String url = new HYContent().GET_SHENG_SHI_QU_URL;
        OkGo.<String>post(url).params(param).tag("GET_SHENG_SHI_QU_URL").execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                List<LocationBean> bean= JSON.parseArray(response.body(),LocationBean.class);
                if (type==0){
                    shengList.clear();
                    shengList.addAll(bean);
                    shengAdapter.notifyDataSetChanged();
                }else if (type==1){
                    shiList.clear();
                    shiList.addAll(bean);
                    shiAdapter.notifyDataSetChanged();
                }
                DialogUtil.hideProgress();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                DialogUtil.hideProgress();
            }
        });
    }

    public interface IEditAreaClick{
        void editArearClick(String city,String cityCode);
    }

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.iv_close){
            bsd1.dismiss();
        }else if (v.getId()==R.id.btn_ok){
            bsd1.dismiss();
            if (locationBean==null){
                ToastUtils.show(mContext,"请选择到市后再试");
                return;
            }
            defaultLocation=locationBean.getName();
            MyApplication.editor.putString(HYContent.LOCATION, defaultLocation);
            MyApplication.editor.commit();
            callBack.editArearClick(defaultLocation,locationBean.getCode());
        }
    }
}
